<?php
namespace Util;
class RegexTool{
	private $validate = array(
		'require'  => '/.+/',
		'email'    => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',//@\w+([-.]\w+)*检测@qq
		//'url'      => '/^http(s?):\/\/(?:[\w\-]+\.)+[A-Za-z]{2,4}$/',//?:是 不想被捕获的时候使用 可以提高程序执行速度
		'currency' => '/^\d+(\.\d+)?$/',
		'number'   => '/^\d+$/',
		'zip'      => '/^\d{6}$/',
		'integer'  => '/^[-\+]?\d+$/',
		'double'   => '/^[-\+]?\d+(\.\d+)?$/',
		'english'  => '/^[A-Za-z]+$/',
		'mobile'   => '/^1(3|4|5|7|8)\d{9}$/',
	);
	
	private $fixMode= null;
	private $matches = array();

	public function __construct($fixMode = null){
		$this->fixMode = $fixMode;
	}

	//获取匹配的数据
	public function getRegexMatchData($pattern,$subject){
		$this->regex($pattern,$subject);
		return $this->matches;
	}

	//获取匹配的判断结果
	private function regex($pattern,$subject){
		if(array_key_exists(strtolower($pattern),$this->validate)){
			$pattern = $this->validate[$pattern].$this->fixMode;
		}
		$isMatch = (preg_match_all($pattern, $subject, $this->matches) > 0) ? true : false;
		return $isMatch;
	}

	//设置修正模式
	public function setFixMode($fixMode){
		$this->fixMode = $fixMode;
	}

	//是否必填
	public function isNotEmpty($str){
		return $this->regex('require',$str);
	}

	//是否email 
	public function isEmail($email){
		return $this->regex('email',$email);
	}

	//是否手机号
	public function isMobile($mobile){
		return $this->regex('mobile',$mobile);
	}

	//自定义的正则表达式
	public function check($pattern,$subject){
		return $this->regex($pattern,$subject);
	}
}
